package com.moregold.mvvmlib.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 *  Created by Yang on 2020/12/8 0008 17:28.
 *
 */
abstract class BaseVMFragment<VB: ViewBinding>(@LayoutRes val layoutId: Int) : Fragment(layoutId) {

    lateinit var binding: VB

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
        binding = method.invoke(null,layoutInflater,container,false) as VB
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        startObserve()
        initData()
        initEvent()
        super.onViewCreated(view, savedInstanceState)
    }


    abstract fun initData()
    abstract fun initEvent()
    abstract fun startObserve()

}